package com.example.guiguiapplicatio.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.guiguiapplicatio.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//根据EditText判断按钮是否可点击
public class EditIsCanUseBtnUtils {

    private List<EditText> editTextList = new ArrayList<>();
    private List<Integer> emptyIdList = new ArrayList<>();
    private List<EditText> emptyEditTextList = new ArrayList<>();


    public static EditIsCanUseBtnUtils getInstance() {
        return new EditIsCanUseBtnUtils();
    }

    public EditIsCanUseBtnUtils addEdittext(EditText editText) {
        editTextList.add(editText);
        return this;
    }

    public Boolean build() {
        setWatcher();
        return checkBtn();
    }

    private void setWatcher() {

        for (int j = 0; j < editTextList.size(); j++) {
            final int i1 = j;
            editTextList.get(j).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 0) {
                        editTextList.get(i1).setBackgroundResource(R.drawable.edittext_min_empty);
                    } else {
                        editTextList.get(i1).setBackgroundResource(R.drawable.edittext_min);

                    }
                }
            });
        }

    }


    private boolean checkBtn() {
        emptyIdList.clear();
        for (int i = 0; i < editTextList.size(); i++) {
            if (editTextList.get(i).getText().length() == 0) {

                emptyEditTextList.add(editTextList.get(i));
                editTextList.get(i).setText("");
            }
        }
        Log.e("onBTN", "onBTN lenth:  "+emptyEditTextList.size());
        if (emptyEditTextList.size() != 0) {
            return true;  //有空的
        } else {
            return false;
        }
    }
}
